package demo.hdz.testui;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import static android.widget.ImageView.ScaleType.FIT_CENTER;


/**
 *
 * @author hdz
 * @date 2018/7/28
 *
 * 参考：
 *
 * https://blog.csdn.net/qq_16064871/article/details/50551437
 * https://blog.csdn.net/carrey1989/article/details/11757409
 * https://www.jb51.net/article/121876.htm
 * https://www.jb51.net/article/121873.htm
 */
public class ImageTextView extends LinearLayout {

    private ImageView mImageView = null;
    private TextView mTextView = null;

    private int imageId;
    private int pressImageId;

    private int textSize = 20;
    private String text;
    private int textColor = Color.GRAY;
    private int pressTextColor = Color.BLACK;
    private boolean bSetPressTextColor = false;
    private int textTop = 10;

    private int imageWidth = LayoutParams.WRAP_CONTENT;
    private int imageHeight = LayoutParams.WRAP_CONTENT;

    private int imageAlpha = 255;
    private float textAlpha = 1.0f;

    public ImageTextView(Context context) {
        this(context, null);
    }

    public ImageTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        this.setOrientation(LinearLayout.VERTICAL);
        this.setGravity(Gravity.CENTER);

        if (mImageView == null) {
            mImageView = new ImageView(context);
        }
//        LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        lp0.gravity = Gravity.CENTER;
//        mImageView.setLayoutParams(lp0);
        addView(mImageView);

        if (mTextView == null) {
            mTextView = new TextView(context);
        }
        LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp2.gravity = Gravity.CENTER;
        mTextView.setLayoutParams(lp2);
        addView(mTextView);
    }

    private void getAttrValues(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        if (attrs == null) {
            return;
        }
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.imageText, defStyleAttr, 0);
        int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {

            int attr = typedArray.getIndex(i);
            Log.d(ImageTextActivity.TAG, "attrName = " + attrs.getAttributeName(i) + " attr="+attr);

            switch (attr) {
                case R.styleable.imageText_imageWidth:
                    imageWidth = typedArray.getDimensionPixelSize(
                            attr, (int) TypedValue.applyDimension(
                                    TypedValue.COMPLEX_UNIT_PX, 25, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.imageText_imageHeight:
                    imageHeight = typedArray.getDimensionPixelSize(
                            attr, (int) TypedValue.applyDimension(
                                    TypedValue.COMPLEX_UNIT_PX, 25, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.imageText_image:
                    imageId = typedArray.getResourceId(attr, 0);
                    break;
                case R.styleable.imageText_pressImage:
                    pressImageId = typedArray.getResourceId(attr, 0);
                    break;

                case R.styleable.imageText_text:
                    text = typedArray.getString(attr);
                    break;
                case R.styleable.imageText_textSize:
                    textSize = typedArray.getDimensionPixelSize(
                            attr, (int) TypedValue.applyDimension(
                                    TypedValue.COMPLEX_UNIT_SP, 25, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.imageText_textColor:
                    textColor = typedArray.getColor(attr, 0);
                    break;
                case R.styleable.imageText_pressTextColor:
                    pressTextColor = typedArray.getColor(attr, 0);
                    this.setTextPressColor(pressTextColor);
                    break;
                case R.styleable.imageText_textTop:
                    textTop = typedArray.getDimensionPixelSize(
                            attr, (int) TypedValue.applyDimension(
                                    TypedValue.COMPLEX_UNIT_PX, 10, getResources().getDisplayMetrics()));
                    break;
                default:
                    break;
            }
        }
        typedArray.recycle();
    }


    public ImageTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.setOrientation(LinearLayout.VERTICAL);
        this.setGravity(Gravity.CENTER);
        if (mImageView == null) {
            mImageView = new ImageView(context);
        }
        if (mTextView == null) {
            mTextView = new TextView(context);
        }

        getAttrValues(context, attrs, defStyleAttr);

        init();
    }


    private void init() {
        this.setText(text);
        this.setTextColor(textColor);
        this.setTextSize(textSize);
        this.setTextPaddingTop(textTop);

        this.setImgResource(imageId);
        this.setImgResourcePress(pressImageId);

        mImageView.setScaleType(FIT_CENTER);
        mImageView.setLayoutParams(new LayoutParams(imageWidth,imageHeight));

        //addView(mImageView);
        //addView(mTextView);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(ImageTextActivity.TAG, event.toString());
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (pressImageId == 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        imageAlpha = mImageView.getImageAlpha();
                        mImageView.setImageAlpha(180);
                    }
                } else {
                    this.setImgResource(pressImageId);
                }
                if (bSetPressTextColor) {
                    mTextView.setTextColor(pressTextColor);
                } else {
                    textAlpha = mTextView.getAlpha();
                    mTextView.setAlpha(0.7f);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (pressImageId == 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        mImageView.setImageAlpha(imageAlpha);
                    }
                } else {
                    if (imageId != 0) {
                        this.setImgResource(imageId);
                    }
                }
                Log.d(ImageTextActivity.TAG, "bSetPressTextColor = " + bSetPressTextColor);
                if (bSetPressTextColor) {
                    mTextView.setTextColor(textColor);
                } else {
                    mTextView.setAlpha(textAlpha);
                }
                break;
            default:
                return super.onTouchEvent(event);
        }
        super.onTouchEvent(event);
        return true;
    }


    /**
     * 设置按下的图片
     *
     * @param resourceID 图片id
     */
    public void setImgResourcePress(int resourceID) {
        pressImageId = resourceID;
    }


    /**
     * 设置显示的图片
     *
     * @param resourceID 图片ID
     */
    private void setImgResource(int resourceID) {
        this.mImageView.setImageResource(resourceID);
    }


    /**
     * 设置显示的文字
     *
     * @param resourceID 字符串ID
     */
    public void setText(int resourceID) {
        this.mTextView.setText(resourceID);
    }
    public void setText(String text) {
        this.mTextView.setText(text);
    }


    /**
     * 设置文本的颜色
     *
     * @param color 颜色值
     */
    public void setTextColor(int color) {
        this.mTextView.setTextColor(color);
    }
    public void setTextColor(ColorStateList colors) {
        this.mTextView.setTextColor(colors);
    }


    /**
     * 设置按下文本的颜色
     *
     * @param color 颜色值
     */
    public void setTextPressColor(int color) {
        pressTextColor = color;
        bSetPressTextColor = true;
    }


    /**
     * 设置字体大小
     *
     * @param size 文本字体大小
     */
    public void setTextSize(float size) {
        this.mTextView.setTextSize(size);
    }


    /**
     * 设置文字与上面的距离
     * @param top 图片与文字直接的间隔
     */
    public void setTextPaddingTop(int top) {
        this.mTextView.setPadding(0, top, 0, 0);
    }


    public void setImageBitmap(Bitmap bitmap) {
        mImageView.setImageBitmap(bitmap);
    }
    public void setImageSize(int width, int height) {
/*        ViewGroup.LayoutParams params = mImageView.getLayoutParams();
        params.height = height;
        params.width  = width;
        mImageView.setLayoutParams(params);*/

        //等比缩放居中显示
        mImageView.setScaleType(FIT_CENTER);
        mImageView.setLayoutParams(new LayoutParams(width,height));
    }
}

